<?php
namespace App\Service\Admin;

class PeizhenService extends BaseService
{
    //陪诊

    //列表
    public function getIndex($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbPeizhen)->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $statuss = $this->peizhen_statuss;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            //患者
            $patOne = (array) $this->dbConn($this->dbUser)
                ->where('del', 0)
                ->where('id', $v['pat_uid'])
                ->first();
            $pat_name = '-';
            $pat_phone = '-';
            if (!empty($patOne)) {
                $pat_name = $patOne['uname'];
                $pat_phone = $patOne['phone'];
            }
            //医院
            $yyOne = (array) $this->dbConn($this->dbYiyuan)
                ->where('del', 0)
                ->where('id', $v['yiyuan'])
                ->first();
            $yiyuan_name = '';
            if (!empty($yyOne)) {
                $yiyuan_name = $yyOne['title'];
            }
            $status_name = array_key_exists($v['status'], $statuss) ? $statuss[$v['status']] : '-';
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'pat_name' => $pat_name,
                'pat_phone' => $pat_phone,
                'doc_name' => '-',
                'start_time' => $v['start_time'],
                'end_time' => $v['end_time'],
                'delay' => $v['delay'].'小时',
                'yiyuan_name' => $yiyuan_name,
                'price' => toNumber($v['price']),
                'status' => $v['status'],
                'status_name' => $status_name,
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //详情
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbPeizhen)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $statuss = $this->peizhen_statuss;
        //患者
        $patOne = (array) $this->dbConn($this->dbUser)
            ->where('del', 0)
            ->where('id', $res['pat_uid'])
            ->first();
        $pat_name = '-';
        $pat_phone = '-';
        if (!empty($patOne)) {
            $pat_name = $patOne['uname'];
            $pat_phone = $patOne['phone'];
        }
        //医院
        $yyOne = (array) $this->dbConn($this->dbYiyuan)
            ->where('del', 0)
            ->where('id', $res['yiyuan'])
            ->first();
        $yiyuan_name = '';
        if (!empty($yyOne)) {
            $yiyuan_name = $yyOne['title'];
        }
        $status_name = array_key_exists($res['status'], $statuss) ? $statuss[$res['status']] : '-';
        $arys = [
            'id' => $res['id'],
            'title' => $res['title'],
            'pat_name' => $pat_name,
            'pat_phone' => $pat_phone,
            'doc_name' => '-',
            'start_time' => $res['start_time'],
            'end_time' => $res['end_time'],
            'delay' => $res['delay'].'小时',
            'yiyuan_name' => $yiyuan_name,
            'price' => toNumber($res['price']),
            'status' => $res['status'],
            'status_name' => $status_name,
            'addtime' => $res['addtime'],
            'robtime' => $res['robtime'] ? $res['robtime'] : '-',
            'closetime' => $res['closetime'] ? $res['closetime'] : '-',
            'finishtime' => $res['finishtime'] ? $res['finishtime'] : '-',
        ];
        return $this->toback(0, '', $arys);
    }
}